package zi.fu.fan.zhuan;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.*;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by Admin on 1/5 0005.
 */
public class TestFiles {
    public static void main(String[] args) {
        File dir = new File("D:\\mystudy");
        showDir(dir);
    }

    private static void showDir(File file) {
        System.out.println("文件开始: " + file);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                showDir(files[i]);
            } else {
                System.out.println(files[i]);
            }
        }
    }

    public void sop(Object obj) {
        System.out.println(obj);
    }

    @Test
    public void test() throws Exception {
        File file = new File("d:\\money\\ye\\" + "zb.txt");
        System.out.println(FileUtils.getFile(file));//d:\money\ye\zb.txt
        //获取系统的临时文件路径
        sop(FileUtils.getTempDirectory());//C:\Users\Admin\AppData\Local\Temp
        //获取代表系统的临时文件路径
        sop(FileUtils.getTempDirectoryPath());//C:\Users\Admin\AppData\Local\Temp\
        //获取用户的主目录路径
        sop(FileUtils.getUserDirectory());//C:\Users\Admin
        //获取代表用户主目录的路径
        sop(FileUtils.getUserDirectoryPath());//C:\Users\Admin

        File file1 = new File("a.txt");
        //根据指定的文件获取一个新的文件输入流
        FileInputStream fis = new FileInputStream(file1);
        sop(FileUtils.openInputStream(file1) == fis);

        //清空目录,但是不删除目录
//        File f=new File("d:\\xinjian");
//        FileUtils.cleanDirectory(f);

        //比较两个文件的内容是否相同
        boolean b = FileUtils.contentEquals(new File("d:\\xinjian\\a.txt"),
                new File("d:\\xinjian\\b.txt"));
        sop(b);
        //讲一个目录内容拷贝到另一个目录中,
        FileUtils.copyDirectory(new File("d:\\xinjian"),
                new File("d:\\xinjian2"), true);

        //将一个文件拷贝到一个新的地址,文件对文件
        FileUtils.copyFile(new File("d:\\xinjian\\a.txt"),
                new File("d:\\xinjian2\\c.txt"), true);
        //将一个文件拷贝到某个目录下文件对目录
        FileUtils.copyFileToDirectory(new File("d:\\xinjian\\b.txt"),
                new File("d:\\xinjian3"));
        //将一个输入流中的内容拷贝到某个文件
        FileInputStream fis1 = new FileInputStream(new File("d:\\xinjian\\a.txt"));
        FileUtils.copyInputStreamToFile(fis1, new File("d:\\xinjian\\aa.txt"));
        //删除目录
        FileUtils.deleteDirectory(new File("d:\\xinjian\\01"));
        //删除文件
        FileUtils.deleteQuietly(new File("d:\\xinjian2\\c.txt"));
        //列出指定目录下的所有文件
        File[] f = new File("d:\\xinjian").listFiles();
        for (File ff : f) {
            sop(ff);
        }
        InputStream in = FileUtils.openInputStream(new File("d:\\xinjian\\a.txt"));
        FileUtils.copyInputStreamToFile(in, new File("d:\\xinjian\\bb.txt"));

        //将文件的内容作为字符串返回
        String str = FileUtils.readFileToString(new File("d:\\xinjian\\a.txt"));
        sop(str);///asdfasdfasdfasdfasdf

        //将文件内容按行返回到一个字符串数组中
        List<String> strs = FileUtils.readLines(new File("d:\\xinjian\\b.txt"));
        for (String s : strs) {
            sop("字符串: " + s);
        }

        //返回文件或目录的大小
        long l1 = FileUtils.sizeOf(new File("d:\\xinjian\\a.txt"));
        long l11 = FileUtils.sizeOfDirectory(new File("d:\\xinjian"));
        long l2 = FileUtils.sizeOf(new File("d:\\xinjian"));
        BigInteger l22 = FileUtils.sizeOfDirectoryAsBigInteger(new File("d:\\xinjian"));
        sop(l1);
        sop(l11);
        sop(l2);
        sop(l22);
        //将字符串内容直接写入到文件中,如果源文件存在,会覆盖
        FileUtils.write(new File("d:\\xinjian\\cc.txt"),
                new String("12344523452345"), "utf8");
    }

    @Test
    public void test7() {
        File src = new File("d:\\xinjian\\a.txt");
        File desc = new File("d:\\xinjian\\copy.txt");
        String str = null;
        try {
            str = FileUtils.readFileToString(src, "utf8");
            FileUtils.writeStringToFile(desc, str, "utf8");
        } catch (IOException e) {
            e.printStackTrace();
        }


        String str1=null;
        try {
            str1=FileUtils.readFileToString(src,"utf8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        str1+="HelloWord!";
        try {
            FileUtils.writeStringToFile(src,str1,"utf8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
